<%@page import="com.carple.dto.LoveBoardComment"%>
<%@page import="com.carple.dto.Member"%>
<%@page import="com.carple.dto.LoveBoard"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%!
String isCommentEditable(Member member, String writer) {	
	//Member member = (Member)session.getAttribute("loginuser");
	if (member == null) return "none";
	
	if (member.getEmailId().equals(writer))
		return "block";
	else
		return "none";
}
%>
<!DOCTYPE html>


<html>
<head>
	<meta charset="utf-8" />
	<title>글쓰기</title>
	<link rel="Stylesheet" href="/carple/styles/main.css" /> 
	<link rel="Stylesheet" href="/carple/styles/default.css" />
	<link rel="Stylesheet" href="/carple/styles/input2.css" />
	<script type="text/javascript">
	function doDelete(boardno){
		var ok = confirm("게시물을 삭제합니다.");//confirm(): ok->true cancel->false 반환 
		if(ok){
			//삭제 처리
			location.href = "delete.action?boardno=" + boardno;
		}
		
	}
	function deleteComment(commentNo, boardNo, pageNo) {
		if (confirm(commentNo + "번 댓글을 삭제할까요?")) {
			location.href='deletecomment.action?commentno=' 
				+ commentNo + "&boardno=" + boardNo + "&pageno=" + pageNo;
		}
	}
	
	function toggleCommentStatus(commentNo, edit) {		
		document.getElementById('commentview' + commentNo).style.display = edit ? 'none' : 'block';
		document.getElementById('commentedit' + commentNo).style.display = edit ? 'block' : 'none';
	}
	function showRouteWindow(emailid){
		window.open("/carple/route/showroute.action?emailid=" + emailid ,"경로보기", 
					 	"width=600px, height=400px, left=30, top=30, resizable=no,fullscreen=no");
	}
	</script>

</head>
<body>
	<% pageContext.include("/WEB-INF/views/include/header.jsp"); %>
	<br /><br /><br /><br /><br />
	<div id="pageContainer">
		<div style="padding-top:25px;text-align:center">
		<div id="inputcontent">
		    <div id="inputmain">
		    <% LoveBoard board = (LoveBoard)request.getAttribute("loveboard"); %>
		    <% String emailId = board.getEmailid(); %>
		        <div class="inputsubtitle" >
		        		<div style="text-align: left; float:left;">
					        게시판 글 보기[<%= board.getHeader() %>]
						</div>
			        
				        <div style="text-align: right; float:right; margin-top: 0px">
					        <input id="showRoute" type="button" value="경로보기" 
							style="width:100px;height:25px"
							onclick="showRouteWindow('<%=emailId%>');" >
						</div>
					</div>
				<%if (board != null) { %>
		        <table>
		            <tr>
		                <th>제목</th>
		                <td><%=board.getTitle() %></td>
		            </tr>
		            <tr>
		                <th>작성자</th>
		                <td><%=emailId%></td>
		            </tr>
		            <tr>
		                <th>작성일</th>
		                <td><%=board.getRegDate() %></td>
		            </tr>
					<tr>
		                <th>조회수</th>
		                <td><%=board.getReadCount() %></td>
		            </tr>
		            <tr>
		                <th>내용</th>
		                <td style="height:200px;vertical-align:top">		                    
		                    <%=board.getContent().replace("\r\n", "<br />") %>
		                    <!-- //TextArea에서는 /r/n이 처리가 되지만 일반 영역에서는 처리가 안됨 -->
		                </td>
		            </tr>
		            
		        </table>
		        <%} %>
		        
		        
		        <div class="buttons">
		        <%
		        	String pageNo = request.getParameter("pageno");
		        	if(pageNo == null || pageNo.length() == 0)
		        		pageNo = "1";
		        %>
		        <% Member member = (Member)session.getAttribute("loginuser"); %>
		        <%if(member.getEmailId().equals(board.getEmailid())){ %>
		        [&nbsp;<a href="editform.action?boardno=<%= board.getBoardNo() %>&pageno=<%= pageNo %>">편집</a>&nbsp;]
		        [&nbsp;<a href="javascript:doDelete(<%= board.getBoardNo() %>)">삭제</a>&nbsp;]
		        <%} %>
		        <%-- [&nbsp;<a href="replyform.action?boardno=<%= board.getBoardNo() %>&pageno=<%= pageNo %>">댓글</a>&nbsp;] --%>
	        	[&nbsp;<a href='list.action?pageno=<%=pageNo%>'>목록보기</a>&nbsp;]
		        </div>
		    </div>
		</div>   	
		
				<!-- comment 쓰기 영역 -->
		<br /><br />
		
		<%-- 로그인 한 사용자만 댓글을 쓸 수 있습니다. --%>
		<% if (session.getAttribute("loginuser") != null) { %>
		<form id="commentform" 
			action="writecomment.action" method="post">
			<input type="hidden" name="boardno"
				value="<%= board.getBoardNo() %>" />
			<input type="hidden" name="pageno"
				value="<%= pageNo %>" />
			<table style="width:600px;border:solid 1px;margin:0 auto">
	            <tr>
	                <td style="width:550px">
	                	
	                    <textarea name="content" 
	                    	style="width:550px" rows="3"></textarea>
	                    
	                </td>
	                <td style="width:50px;vertical-align:middle">
	                	<a href="javascript:document.getElementById('commentform').submit();"
	                		style="text-decoration:none">
	                		댓글<br />등록
	                	</a>
	                </td>
	            </tr>                    
	        </table>
        </form>
        <% } %>
        <br />
        
        <div style="margin:0 auto;width:600px">
        <hr align="center" style="width:600px; text-align:center"/>
        </div>
        
        <br />
        <% if (board.getComments().size() == 0) { %>
        <h3 id="nodata" style="text-align:center">
            작성된 댓글이 없습니다.
        </h3>
        <% } else { %>        
		<!-- comment 표시 영역 -->
		<table style="width:600px;border:solid 1px;margin:0 auto">
		<% for(LoveBoardComment bcomment : board.getComments()) { %>
        	<tr>
        		<td style="text-align:left;margin:5px;border-bottom: solid 1px">
        		<div id='commentview<%= bcomment.getCommentNo() %>'>
                    <%= bcomment.getEmailId() %> &nbsp;&nbsp;
                    [ <%= bcomment.getRegDate() %> ]
                    <br /><br />
                    <span>
                        <%= bcomment.getContent().replace("\r\n", "<br />") %>
                    </span>
                    <br /><br />
                    <div style="display: <%= isCommentEditable((Member)session.getAttribute("loginuser"), bcomment.getEmailId()) %>">
                    	<a href="javascript:toggleCommentStatus(<%= bcomment.getCommentNo() %>, true);">편집</a>
                    	&nbsp;
                    	<a href="javascript:deleteComment(<%= bcomment.getCommentNo() %>, <%= board.getBoardNo() %>, <%= pageNo %>)">삭제</a>
                    </div>
                </div>
                
                <div id='commentedit<%= bcomment.getCommentNo() %>' style="display: none">
					<%= bcomment.getEmailId() %>&nbsp;&nbsp; 
					[<%= bcomment.getRegDate() %>] 
					<br /><br />
					<form id="commenteditform<%= bcomment.getCommentNo() %>" 
						action="updatecomment.action" method="post">
					<input type="hidden" name="boardno"
						value="<%= board.getBoardNo() %>" />
					<input type="hidden" name="pageno"
						value="<%= pageNo %>" />
					<input type="hidden" name="commentno"
						value="<%= bcomment.getCommentNo() %>" />
					<textarea name="content" style="width: 600px" rows="3" 
						maxlength="200"><%= bcomment.getContent() %></textarea>
					</form>
					<div>
						<a href="javascript:document.getElementById('commenteditform<%= bcomment.getCommentNo() %>').submit();">수정</a> 
						&nbsp; 
						<a href="javascript:toggleCommentStatus(<%= bcomment.getCommentNo() %>, false);">취소</a>
					</div>
				</div>
	
				</td>
        	</tr>
        <% } %>
        </table>		
		<% } %>
	<br /><br /><br /><br /><br /><br />
	</div>
	</div>

</body>
</html>





